﻿using System;
using System.Windows.Forms;

namespace State
{
    public partial class SafeFrame : Form, IContext
    {
        private int hour;
        private IState state;

        public SafeFrame()
        {
            InitializeComponent();

            this.hour = 0;
            this.state = DayState.GetInstance();

            buttonUse.Click += new EventHandler(ActionPerformad);
            buttonAlarm.Click += new EventHandler(ActionPerformad);
            buttonPhone.Click += new EventHandler(ActionPerformad);
            buttonExit.Click += new EventHandler(ActionPerformad);
        }


        protected void ActionPerformad(object sender, EventArgs e)
        {
            Console.WriteLine(sender);

            var button = (Button)sender;

            if (button.Name == "buttonUse")
            {
                state.DoUse(this);
            }
            else if (button.Name == "buttonAlarm")
            {
                state.DoAlarm(this);
            }
            else if (button.Name == "buttonPhone")
            {
                state.DoPhone(this);
            }
            else if (button.Name == "buttonExit")
            {
                this.Close();
            }
            else
            {
                Console.WriteLine("?");
            }
        }


        #region IContext メンバ

        public void SetClock(int hour)
        {
            string clockString = "現在時刻は";

            if (hour < 10)
            {
                clockString += "0" + hour + ":00";
            }
            else
            {
                clockString += hour + ":00";
            }

            Console.WriteLine(clockString);
            textClock.Text = clockString;
            state.DoClock(this, hour);
        }

        public void ChangeState(IState state)
        {
            Console.WriteLine(this.state + "から" + state + "へ状態が変化しました。");
            this.state = state;
        }

        public void CallSecurityCenter(string msg)
        {
            textScrean.Text += "Call! " + msg + Environment.NewLine;
        }

        public void RecordLog(string msg)
        {
            textScrean.Text += "Recode... " + msg + Environment.NewLine;
        }

        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            SetClock(hour);

            if (hour < 24)
            {
                hour++;
            }
            else
            {
                hour = 0;
            }
        }
    }
}
